/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.message;

import com.twolattes.json.Entity;
import com.twolattes.json.Json;
import com.twolattes.json.Marshaller;
import com.twolattes.json.TwoLattes;
import com.twolattes.json.Value;
import hextd.message.MessageFactory.NetworkMessage;
import hextd.core.MapInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

@Entity
public class MessageMapList implements Message {

	private static Marshaller<MessageMapList> marshaller;
	public NetworkMessage type = NetworkMessage.MapList;
	public static String key = "MML";
	public static Pattern MessagePattern = Pattern.compile(key + ".*");
	//
	private String message;
	private Map<Integer, MapInfo> mapCache = new HashMap<Integer, MapInfo>();
	@Value
	public List<MapInfo> mapList;
	@Value
	public Map<String, Integer> categories;
	private Map<Integer, String> inverse;

	public static String formatMessage(List<MapInfo> maps, Map<Integer, String> categories) {
		MessageMapList mml = new MessageMapList();
		mml.mapList = maps;
		Map<String, Integer> inverse = new HashMap<String, Integer>(categories.size());
		for (Entry<Integer, String> e : categories.entrySet()) {
			inverse.put(e.getValue(), e.getKey());
		}
		mml.categories = inverse;
		return key + marshaller.marshall(mml).toString();
	}

	public MessageMapList() {
		if (marshaller == null) {
			marshaller = TwoLattes.createMarshaller(MessageMapList.class);
		}
	}

	public Map<Integer, MapInfo> getMapCache() {
		return mapCache;
	}

	public Map<Integer, String> getCategories() {
		return inverse;
	}

	public List<MapInfo> getMapList() {
		return mapList;
	}

	@Override
	public String getMessage() {
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		if (marshaller == null) {
			marshaller = TwoLattes.createMarshaller(MessageMapList.class);
		}
		Json.Value fromString = Json.fromString(message.substring(key.length()));
		MessageMapList unmarshall = marshaller.unmarshall((Json.Object) fromString);
		for (MapInfo m : unmarshall.mapList) {
			unmarshall.mapCache.put(m.mapId, m);
		}
		unmarshall.message = message;
		unmarshall.inverse = new HashMap<Integer, String>();
		for (Entry<String, Integer> e : unmarshall.categories.entrySet()) {
			unmarshall.inverse.put(e.getValue(), e.getKey());
		}
		return unmarshall;
	}

	@Override
	public int getSenderId() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Pattern getPattern() {
		return MessageMapList.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}
}
